﻿using System;

namespace Sgr.SystemInfo.ViewModels
{
    /// <summary>
    /// 系统基础信息
    /// </summary>
    public class SystemBaseInfo
    {
        private static readonly char[] VersionSeparators = new[] { '.', '-' };

        /// <summary>
        /// 应用名称
        /// 不包含内部代号或敏感标识
        /// </summary>
        /// <example>PaymentGateway</example>
        public string Name { get; set; } = string.Empty;

        /// <summary>
        /// 应用版本
        /// 仅显示主要版本号，如v2.x
        /// </summary>
        /// <example>v2.x</example>
        public string Version { get; set; } = string.Empty;

        /// <summary>
        /// 运行环境
        /// 仅显示production或non-production
        /// </summary>
        /// <example>production</example>
        public string Environment { get; set; } = string.Empty;

        /// <summary>
        /// 系统健康状态
        /// healthy - 正常
        /// degraded - 服务降级
        /// unhealthy - 不健康
        /// </summary>
        /// <example>healthy</example>
        public string Status { get; set; } = string.Empty;

        /// <summary>
        /// 运行时长
        /// 以人类可读格式显示，如"30 days"
        /// </summary>
        /// <example>30 days</example>
        public string Uptime { get; set; } = string.Empty;

        /// <summary>
        /// 系统架构
        /// </summary>
        /// <example>x64</example>
        public string Architecture { get; set; } = string.Empty;

        /// <summary>
        /// 处理器核心数
        /// </summary>
        /// <example>8</example>
        public int ProcessorCount { get; set; }

        /// <summary>
        /// 容器环境描述
        /// 可能的值：non-container, docker, kubernetes, other
        /// </summary>
        /// <example>kubernetes</example>
        public string ContainerEnvironment { get; set; } = "non-container";

        /// <summary>
        /// 创建用于显示的版本号
        /// </summary>
        /// <param name="fullVersion">完整版本号</param>
        /// <returns>格式化后的主版本号</returns>
        public static string FormatVersion(string fullVersion)
        {
            if (string.IsNullOrEmpty(fullVersion))
                return "v1.x";

            // 提取主版本号，如1.2.3-beta转换为v1.x
            var version = fullVersion.Split(VersionSeparators)[0];
            return $"v{version}.x";
        }

        /// <summary>
        /// 格式化环境名称
        /// </summary>
        /// <param name="environment">原始环境名称</param>
        /// <returns>production或non-production</returns>
        public static string FormatEnvironment(string environment)
        {
            return environment.Equals("Production", StringComparison.OrdinalIgnoreCase)
                ? "production"
                : "non-production";
        }

        /// <summary>
        /// 格式化运行时长
        /// </summary>
        /// <param name="uptime">运行时间间隔</param>
        /// <returns>人类可读的时间字符串</returns>
        public static string FormatUptime(TimeSpan uptime)
        {
            if (uptime.TotalDays >= 1)
                return $"{(int)uptime.TotalDays} days";
            if (uptime.TotalHours >= 1)
                return $"{(int)uptime.TotalHours} hours";
            if (uptime.TotalMinutes >= 1)
                return $"{(int)uptime.TotalMinutes} minutes";
            return "just started";
        }
    }
}